<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="职级名称" prop="professionRankName">
        <el-input
          v-model="queryParams.professionRankName"
          placeholder="请输入职级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组别名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入组别名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="岗位名称" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入岗位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:salary:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:salary:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:salary:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:salary:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="salaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="长期工工资id" align="center" prop="id" />-->
      <el-table-column fixed="left"  label="姓名" align="center" prop="staffName" />
      <el-table-column label="身份证id" align="center" prop="idCard" width="170" />
      <el-table-column label="班组" align="center" prop="deptName" />
      <el-table-column label="职级" align="center" prop="professionRankName" />
      <el-table-column label="组别" align="center" prop="groupName" />
<!--      <el-table-column label="岗位id" align="center" prop="postId" />-->
      <el-table-column label="岗位名称" align="center" prop="postName" />
      <el-table-column label="月应出勤天数" align="center" prop="needAttendanceNumber" />
      <el-table-column label="实际出勤天数" align="center" prop="actualAttendanceNumber" />
      <el-table-column label="通班出勤天数" align="center" prop="dayNightAttendanceNumber" />
      <el-table-column label="白班出勤天数" align="center" prop="dayAttendanceNumber" />
      <el-table-column label="夜班出勤天数" align="center" prop="nightAttendanceNumber" />
      <el-table-column label="月综合工资标准" align="center" prop="salaryStandard" />
      <el-table-column label="标准工资" align="center" prop="basicSalary" />
      <el-table-column label="基本工资" align="center" prop="jobBasicSalary" />
      <el-table-column label="岗位补贴" align="center" prop="jobSubsidy" />
      <el-table-column label="岗位工资" align="center" prop="jobSalary" />
      <el-table-column label="补发工资" align="center" prop="backPay" />
      <el-table-column label="月度绩效" align="center" prop="monthlyPerformance" />
      <el-table-column label="住房补贴" align="center" prop="housingAllowance" />
      <el-table-column label="试用期系数" align="center" prop="probationPeriodCoefficient" />
      <el-table-column label="全勤奖" align="center" prop="perfectAttendanceAward" />
      <el-table-column label="月度考核奖励" align="center" prop="monthlyAssessmentRewards" />
      <el-table-column label="应发工资" align="center" prop="payable" />
      <el-table-column label="个人养老保险代扣" align="center" prop="personalPension" />
      <el-table-column label="个人医疗保险代扣" align="center" prop="personalMedical" />
      <el-table-column label="个人失业保险代扣" align="center" prop="individualUnemployment" />
      <el-table-column label="社保之和" align="center" prop="sumSocialSecurity" />
      <el-table-column label="月度考核扣款" align="center" prop="assessmentDeduction" />
      <el-table-column label="考勤扣款" align="center" prop="attendanceDeduction" />
      <el-table-column label="工装扣款" align="center" prop="toolingDeduction" />
      <el-table-column label="实发工资" align="center" prop="paidWages" />
      <el-table-column label="备注" align="center" prop="remark" width="180" />
      <el-table-column fixed="right" v-if="isEdit" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pay:list:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改长期工工资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="补发工资" prop="backpay">
              <el-input v-model="form.backPay" placeholder="请输入补发工资" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月度绩效" prop="monthlyPerformance">
              <el-input v-model="form.monthlyPerformance" placeholder="请输入月度绩效" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="全勤奖" prop="perfectAttendanceAward">
              <el-input v-model="form.perfectAttendanceAward" placeholder="请输入全勤奖" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月度考核奖励" prop="monthlyAssessmentRewards">
              <el-input v-model="form.monthlyAssessmentRewards" placeholder="请输入月度考核奖励" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月度考核扣款" prop="assessmentDeduction">
              <el-input v-model="form.assessmentDeduction" placeholder="请输入月度考核扣款" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤扣款" prop="attendanceDeduction">
              <el-input v-model="form.attendanceDeduction" placeholder="请输入考勤扣款" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工装扣款" prop="toolingDeduction">
              <el-input v-model="form.toolingDeduction" placeholder="请输入工装扣款" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark"  placeholder="请输入备注" type="textarea" :rows="2" >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSalaryDetail, getSalary, delSalary, addSalary, updateSalary, exportSalary } from "@/api/personnel/pay/main";

export default {
  name: "Salary",
  data() {
    return {
      isEdit: false,   //操作类型  是否为 编辑
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 长期工工资表格数据
      salaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: null,
        idCard: null,
        deptId: null,
        deptName: null,
        professionRankId: null,
        professionRankName: null,
        groupName: null,
        postId: null,
        postName: null,
        needAttendanceNumber: null,
        actualAttendanceNumber: null,
        dayNightAttendanceNumber: null,
        dayAttendanceNumber: null,
        nightAttendanceNumber: null,
        salaryStandard: null,
        basicSalary: null,
        jobBasicSalary: null,
        jobSubsidy: null,
        jobSalary: null,
        backPay: null,
        monthlyPerformance: null,
        housingAllowance: null,
        probationPeriodCoefficient: null,
        perfectAttendanceAward: null,
        monthlyAssessmentRewards: null,
        payable: null,
        personalPension: null,
        personalMedical: null,
        individualUnemployment: null,
        sumSocialSecurity: null,
        assessmentDeduction: null,
        attendanceDeduction: null,
        toolingDeduction: null,
        paidWages: null,
        workTime: null,
        salarySumId:this.$route.query.id
      },
      // 表单参数
      form: {
        backPay:undefined,
        monthlyPerformance:undefined,
        perfectAttendanceAward:undefined,
        monthlyAssessmentRewards:undefined,
        assessmentDeduction:undefined,
        attendanceDeduction:undefined,
        toolingDeduction:undefined,
        remark:undefined,
      },
      // 表单校验
      rules: {
        backPay: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        monthlyPerformance: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        perfectAttendanceAward: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        monthlyAssessmentRewards: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        assessmentDeduction: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        attendanceDeduction: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        toolingDeduction: [
          { required: true, message: '请输入', trigger: 'change' },
        ],
        remark:[
          { required: false },
          { max: 20, message: '长度最多 20 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    const params = this.$route.query;
    // console.log(params);
    if(params&&params.id){
      // this.queryParams.salarySumId = params.id;
      this.getList();
      //操作类型
      if(params.action && params.action === 'edit'){
        this.isEdit = true;
      }
    }
  },
  methods: {
    /** 查询长期工工资列表 */
    getList() {
      this.loading = true;
      listSalaryDetail(this.queryParams).then(response => {
        // console.log(response)
        this.salaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staffName: null,
        idCard: null,
        deptId: null,
        deptName: null,
        professionRankId: null,
        professionRankName: null,
        groupName: null,
        postId: null,
        postName: null,
        needAttendanceNumber: null,
        actualAttendanceNumber: null,
        dayNightAttendanceNumber: null,
        dayAttendanceNumber: null,
        nightAttendanceNumber: null,
        salaryStandard: null,
        basicSalary: null,
        jobBasicSalary: null,
        jobSubsidy: null,
        jobSalary: null,
        backpay: null,
        monthlyPerformance: null,
        housingAllowance: null,
        probationPeriodCoefficient: null,
        perfectAttendanceAward: null,
        monthlyAssessmentRewards: null,
        payable: null,
        personalPension: null,
        personalMedical: null,
        individualUnemployment: null,
        sumSocialSecurity: null,
        assessmentDeduction: null,
        attendanceDeduction: null,
        toolingDeduction: null,
        paidWages: null,
        workTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加长期工工资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // console.log(row)
      this.reset();
      this.form = {...row};
      this.open = true;
      this.title = "工资单编辑";
      // const id = row.id || this.ids
      // getSalary(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "工资单编辑";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(valid,this.form);
          const { id, payable, paidWages, backPay, monthlyAssessmentRewards,perfectAttendanceAward,monthlyPerformance, assessmentDeduction, attendanceDeduction,toolingDeduction,remark } = this.form;
          const params = { id, payable, paidWages, backPay, monthlyAssessmentRewards,perfectAttendanceAward,monthlyPerformance, assessmentDeduction, attendanceDeduction,toolingDeduction,remark };
          console.log(params);
          updateSalary(params).then(response => {
            // console.log(response)
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          // if (this.form.id != null) {
          //   updateSalary(this.form).then(response => {
          //     this.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //   addSalary(this.form).then(response => {
          //     this.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除长期工工资编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delSalary(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有长期工工资数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportSalary(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
